package utils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询工具
 * @author Administrator
 *
 */
public class QueryUtil {
	public static List convertList(ResultSet rs) throws SQLException {

		List list = new ArrayList();

		ResultSetMetaData md = rs.getMetaData();

		int columnCount = md.getColumnCount(); //Map rowData;

		while (rs.next()) { //rowData = new HashMap(columnCount);

			Map rowData = new HashMap();

			for (int i = 1; i <= columnCount; i++) {

				rowData.put(md.getColumnName(i), rs.getObject(i));

			}

			list.add(rowData);

		} return list;

	}

	/**
	 * 获取当前查询的总记录数
	 * @param em
	 * @return
	 */
	public static int  getQueryCount(EntityManager em){
		int count = 0;
		Query queryCount = em.createNativeQuery("select FOUND_ROWS() as result");
		count = Integer.parseInt(queryCount.getResultList().get(0).toString());
		return count;
	}
}
